package controllers

import (
	// "encoding/json"
	"fmt"
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
	"sesm/models"
	"strconv"
)

// Operations about object
type SystemController struct {
	baseController
}

// @Title 获取区域列表
// @Description 获取所有集群地域配置
// @Success 200 {object} []models.ClusterRegion
// @Failure 403 objectId is empty
// @router /config/regions [get]
func (s *SystemController) GetRegions() {
	o := orm.NewOrm()
	qs := o.QueryTable("cluster_region")
	qs.All(&models.Regions)
	s.normalReturn(&models.Regions)
}

// @Title 新增区域
// @Description 新增区域配置
// @Param	region_code formData string true  "区域代码"
// @Param	region_desc formData string true  "区域描述"
// @Success 201 {string} create success !
// @Failure 422 create failed
// @router /config/regions [post]
func (s *SystemController) AddRegion() {
	region_code := s.GetString("region_code")
	region_desc := s.GetString("region_desc")

	var region models.ClusterRegion
	region.RegionCode = region_code
	region.RegionDesc = region_desc

	o := orm.NewOrm()
	id, err := o.Insert(&region)
	if err != nil {
		fmt.Println(id)
		s.errReturn(err, 422)
	}
	s.normalReturn(id)
}

// @Title 删除区域
// @Description 删除区域配置
// @Param	region_id query string true  "区域编号"
// @Success 204 {string} delete success !
// @Failure 422 delete failed
// @router /config/regions [delete]
func (s *SystemController) DelRegion() {
	region_id := s.GetString("region_id")
	id, err := strconv.Atoi(region_id)
	if err != nil {
		s.errReturn(err, 422)
	}
	o := orm.NewOrm()
	var num int64
	if num, err = o.Delete(&models.ClusterRegion{Id: id}); err != nil {
		s.errReturn(err, 422)
	}
	s.normalReturn(num)
}

// @Title 新增集群类型
// @Description 新增类型
// @Param	type_name formData string true  "类型代码"
// @Param	type_desc formData string true  "类型描述"
// @Success 201 {string} create success !
// @Failure 422 create failed
// @router /config/types [post]
func (s *SystemController) AddTypes() {
	type_name := s.GetString("type_name")
	type_desc := s.GetString("type_desc")

	var types models.ClusterTypes
	types.Name = type_name
	types.Description = type_desc

	o := orm.NewOrm()
	id, err := o.Insert(&types)
	if err != nil {
		fmt.Println(id)
		s.errReturn(err, 422)
	}
	s.normalReturn(id)
}

// @Title 获取类型列表
// @Description 获取所有集群类型配置
// @Success 200 {object} []models.ClusterTypes
// @Failure 403 can not found
// @router /config/types [get]
func (s *SystemController) GetTypes() {
	o := orm.NewOrm()
	qs := o.QueryTable("cluster_types")
	qs.All(&models.Types)
	s.normalReturn(&models.Types)
}

// @Title 删除集群类型
// @Description 删除类型配置
// @Param	types_id query string true  "类型编号"
// @Success 204 {string} delete success !
// @Failure 422 delete failed
// @router /config/types [delete]
func (s *SystemController) DelType() {
	types_id := s.GetString("types_id")
	id, err := strconv.Atoi(types_id)
	if err != nil {
		s.errReturn(err, 422)
	}
	o := orm.NewOrm()
	var num int64
	if num, err = o.Delete(&models.ClusterTypes{Id: id}); err != nil {
		s.errReturn(err, 422)
	}
	s.normalReturn(num)
}
